<template>
  <div id="login">
    <div class="login-box">
      <!-- LOGO部分 -->
      <div class="login-logo">
        <img src="@/assets/img/logo.png" alt="">
      </div>

      <!-- 表单区域 -->
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <!-- 现在在组件上使用 v-on 只会监听自定义事件 (组件用 $emit 触发的事件)。如果要监听根元素的原生事件，可以使用 .native 修饰符 -->
            <el-input prefix-icon="el-icon-s-custom" clearable placeholder="请输入用户名" v-model="loginForm.username" @keyup.enter.native="submit"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input type="password" prefix-icon="el-icon-lock" clearable placeholder="请输入密码" v-model="loginForm.password" @keyup.enter.native="submit"></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="login-button">
            <el-button type="primary" @keyup.enter.native="submit" @click="submit">登录</el-button>
            <el-button type="info" plain @keyup.enter.native="reset" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from 'axios'
const axios = require('axios')
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // Form 组件提供了表单验证的功能，只需要通过 rules 属性传入约定的验证规则，并将 Form-Item 的 prop 属性设置为需校验的字段名即可
      loginRules: {
        username: [// trigger: 触发条件
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 16, message: '用户名长度为2-16位', trigger: 'blur' }
        ],
        password: [// trigger: 触发条件
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 16, message: '用户名长度为2-16位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // const axios = require('axios')
          axios.post('https://easy-mock.com/mock/5f73fa19afffa44aa69cfd23/vue-admin/getUserList').then(
            (res) => {
              console.log(res)
            }
          )
          // this.$router.replace('/')
          // 验证通过发起网络请求判定是否存在该用户
          // const $axios = axios.create({
          //   baseURL: 'https://easy-mock.com/mock/5f73fa19afffa44aa69cfd23/vue-admin',
          //   timeout: 10000, // 过期时间
          //   transformResponse: (data) => {
          //     console.log(data)
          //     return data
          //   }
          // })
          // const config = {
          //   url: '/getUserList',
          //   method: 'post'
          // }
          // const data = $axios(config)
          // console.log(data)
          this.$message({
            message: '登录成功',
            showClose: true,
            center: true,
            type: 'success'
          })
        } else {
          console.log('登录失败')
        }
      })
    },
    reset () {
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
#login {
  position: relative;
  height: 100%;
  background-color: var(--baseColor);
  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 500px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    transform: translate(-50%, -50%);
    .login-logo {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 150px;
        height: 150px;
        background-color: #f6f6f6;
        border: 10px solid #fff;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;
      left: 0;
      padding: 0 10px;
      width: 100%;
      box-sizing: border-box;
      .login-button {
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>
